package acwing._1_1AlgorithmBasic._3SearchGraph;
/**
 * @Project : AlgorithmLearning
 * @Package : ACWing._1_1AlgorithmBasic._3SearchGraph
 * @File : Q0858.java
 * @Author : WangRuoyu
 * @Date : 2023/4/3 10:47
 */

import java.io.*;
import java.util.Arrays;

// 朴素prim
public class Q0858 {
    static int prim(int[][] dis) {
        boolean[] st = new boolean[dis.length];
        int[] d = new int[dis.length];
        Arrays.fill(d, 0x3f3f3f3f);
        int res = 0;
        for (int i = 0; i < dis.length; ++i) { // 选择n个点，n-1条边
            int idx = -1;
            for (int j = 0; j < d.length; ++j) { // 选择距离连通图最近的点
                if ((idx == -1 || d[idx] > d[j]) && !st[j]) {
                    idx = j;
                }
            }
            if (i != 0 && d[idx] == 0x3f3f3f3f) {
                res = 0x3f3f3f3f;
                break;
            }
            st[idx] = true;
            if (i != 0) res += d[idx];

            for (int j = 0; j < dis.length; ++j) {
                d[j] = Math.min(d[j], dis[idx][j]); // 与dijkstra算法不同，更新到集合的距离，是dis[idx][j]而不是d[j]+dis[idx][j]
            }
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] words = br.readLine().split(" ");
        int n = Integer.parseInt(words[0]);
        int m = Integer.parseInt(words[1]);
        int[][] dis = new int[n][n];

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (i == j) dis[i][j] = 0;
                else dis[i][j] = 0x3f3f3f3f;
            }
        }

        for (int i = 0; i < m; ++i) {
            words = br.readLine().split(" ");
            int a = Integer.parseInt(words[0]) - 1;
            int b = Integer.parseInt(words[1]) - 1;
            int c = Integer.parseInt(words[2]);
            if (a != b) {
                dis[a][b] = Math.min(dis[a][b], c);
                dis[b][a] = Math.min(dis[b][a], c);
            }
        }

        int res = prim(dis);
        if (res == 0x3f3f3f3f) {
            wr.write("impossible\n");

        } else {
            wr.write(prim(dis) + "\n");
        }
        wr.flush();
    }
}